import Vue from 'vue'

export const SET_MY_INFO = 'SET_MY_INFO' //我的基本信息
export const USER_SIGNOUT = 'USER_SIGNOUT' //退出登录

export const UPDATE_MY_DESC = 'UPDATE_MY_DESC' //更新签名
export const UPDATE_MY_NICKNAME = 'UPDATE_MY_NICKNAME' //更新签名
export const UPDATE_MY_TAG_LIST = 'UPDATE_MY_TAG_LIST' //更新签名
export const UPDATE_MY_AVATAR = 'UPDATE_MY_AVATAR' //更新头像

export default {
  state: JSON.parse(localStorage.getItem('myInfo')) || {},
  getters: {
    genderStr: state => {
      var str = '未知';
      if (1 === state.gender) {
        str = '男';
      } else if (0 === state.gender) {
        str = '女';
      }
      return str;
    }
  },
  mutations: {
    [USER_SIGNOUT](state) {
      localStorage.removeItem('myInfo')
      Object.keys(state).forEach(k => Vue.delete(state, k))
    },

    [SET_MY_INFO](state, myInfo) {
      // console.log('mutation SET_MY_INFO');
      localStorage.setItem('myInfo', JSON.stringify(myInfo))
      Object.assign(state, myInfo) //把user的属性复制给state
    },

    [UPDATE_MY_DESC](state, desc) {
      state.description = desc;
      localStorage.setItem('myInfo', JSON.stringify(state))
      // console.log(state);
    },

    [UPDATE_MY_NICKNAME](state, nickname) {
      state.nickname = nickname;
      localStorage.setItem('myInfo', JSON.stringify(state))
      // console.log(state);
    },

    [UPDATE_MY_TAG_LIST](state, tagList) {
      state.tagList = tagList;
      localStorage.setItem('myInfo', JSON.stringify(state))
    },

    [UPDATE_MY_AVATAR](state, avatarUrl) {
      state.avatar = avatarUrl;
      localStorage.setItem('myInfo', JSON.stringify(state))
    },

  },
  actions: {
    [SET_MY_INFO]({
      commit
    }, myInfo) {
      commit(SET_MY_INFO, myInfo)
    },

    [USER_SIGNOUT]({
      commit
    }) {
      commit(USER_SIGNOUT)
    },

    [UPDATE_MY_DESC]({
      commit
    }, desc) {
      commit(UPDATE_MY_DESC, desc)
    },

    [UPDATE_MY_NICKNAME]({
      commit
    }, nickname) {
      commit(UPDATE_MY_NICKNAME, nickname)
    },

    [UPDATE_MY_TAG_LIST]({
      commit
    }, tagList) {
      commit(UPDATE_MY_TAG_LIST, tagList)
    },

    [UPDATE_MY_AVATAR]({
      commit
    }, avatarUrl) {
      commit(UPDATE_MY_AVATAR, avatarUrl)
    }
  }
}
